package com.common.app.widget.calendarPickerView

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import com.common.app.R
import com.common.app.data.bean.CalendarDay
import com.common.app.databinding.CalendarDayItemLayoutBinding
import com.common.base.utils.ViewUtils.getDrawable

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：17/7/2025
 *
 * 描述：日历单元格视图 - 使用XML布局
 *
 * 修订历史：
 *
 */
class CalendarDayNewView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private val mViewBinding = CalendarDayItemLayoutBinding.inflate(LayoutInflater.from(context), this, true)
    private var calendarDay: CalendarDay? = null


    // 颜色资源
    private val normalTextColor = ContextCompat.getColor(context, R.color.text_content)
    private val weekTextColor = ContextCompat.getColor(context, R.color.text_content_week)
    private val selectedTextColor = ContextCompat.getColor(context, R.color.white)
    private val todayTextColor = ContextCompat.getColor(context, R.color.product_color)

    init {
    }

    fun setCalendarDay(day: CalendarDay) {
        this.calendarDay = day
        updateView()
    }

    private fun updateView() {
        val day = calendarDay ?: return

        // 如果不是当前月份的日期，隐藏所有内容
        if (!day.isCurrentMonth) {
            mViewBinding.tvDayNumber.visibility = View.INVISIBLE
            mViewBinding.viewEventDot.visibility = View.INVISIBLE
            mViewBinding.viewSelectedBackground.visibility = View.GONE
            return
        }

        // 显示日期文字
        mViewBinding.tvDayNumber.visibility = View.VISIBLE
        mViewBinding.tvDayNumber.text = day.day.toString()
        // 绘制事件标记点
//        mViewBinding.viewEventDot.visibility = if (!day.isCurrentMonth) View.INVISIBLE else View.VISIBLE
        mViewBinding.viewEventDot.visibility = View.VISIBLE
        mViewBinding.viewEventDot.background = if (day.hasEvent) getDrawable(R.drawable.bg_round_product_100) else getDrawable(R.drawable.bg_round_ebebeb_100)

        // 设置文字颜色和背景
        when {
            day.isSelected -> {
                mViewBinding.tvDayNumber.setTextColor(selectedTextColor)
                mViewBinding.viewSelectedBackground.visibility = View.VISIBLE
            }
            day.isToday -> {
                mViewBinding.tvDayNumber.setTextColor(todayTextColor)
                mViewBinding.viewSelectedBackground.visibility = View.GONE
            }
            else -> {
                mViewBinding.tvDayNumber.setTextColor(normalTextColor)
                mViewBinding.viewSelectedBackground.visibility = View.GONE
            }
        }
    }
}